package algotithm.leetcode.bytedance.test3;

/**
 * @author zhouyanxiang
 * @Date 2021-03-2021/3/2-22:41
 * @URL https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
 * @Title 3. 无重复字符的最长子串
 */
public class Solution {

    public static void main(String[] args) {
        String s = "abcabcbb";
        Solution solution = new Solution();
        int length = solution.lengthOfLongestSubstring(s);
        System.out.println(length);
    }

    public int lengthOfLongestSubstring(String s) {
        char[] c = s.toCharArray();
        int[] cnt = new int[128];
        int n = s.length();
        for (int i = 0; i < n; i++) {
            cnt[c[i]] = -1;
        }
        int l = 0;
        int res = 0;
        for (int i = 0; i < n; i++) {
            if (cnt[c[i]] != -1) {
                l = Math.max(l,cnt[c[i]] + 1);
            }
            cnt[c[i]] = i;
            res = Math.max(res,i - l + 1);
        }
        return res;

    }

}
